package com.higgins.lawapp.client.settings.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.higgins.lawapp.client.listeners.DataSourceDTOListBoxListener;
import com.higgins.lawapp.client.listeners.DataSourceDTOsListener;
import com.higgins.lawapp.client.settings.components.ButtonsPanel;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemLevelDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceDTO;
import com.higgins.lawapp.client.settings.dataModel.DataSourceLevelDTO;
import com.higgins.lawapp.client.settings.presenter.DataSourcePresenter;
import com.higgins.lawapp.client.settings.rpc.DataSourceService;
import com.higgins.lawapp.client.settings.rpc.DataSourceServiceAsync;
import com.higgins.lawapp.client.settings.rpc.InternetService;
import com.higgins.lawapp.client.settings.rpc.InternetServiceAsync;


public class DataSourceModel implements DataSourceDTOListBoxListener	{
	private final DataSourceServiceAsync dataSourceService = GWT.create(DataSourceService.class);	
	private final InternetServiceAsync internetService = GWT.create(InternetService.class);	
	private List<DataSourceDTOsListener> dataSourceDTOsListeners = new ArrayList<DataSourceDTOsListener>();
	private List<DataSourceDTO> dataSourceDTOs;
	private DataSourceDTO dataSourceDTOSelected;
	private DataSourceDTO newDataSourceDTO;
	private DataSourcePresenter dataSourcePresenter;
	private String elementListItem;
	private String elementName;
	private ArrayList<String> elementListItems;
	private int dataSourceSelectedIndex;
	private DataItemLevelDTO dataItemsDTO;
	private ButtonsPanel buttons;
	
	public DataSourceModel()	{
	}
	public void setNewDataSourceDTO(DataSourceDTO dataSourceDTO)	{
		newDataSourceDTO = dataSourceDTO;
	}
	public DataSourceDTO getDataSourceDTOSelected()	{
		if (dataSourceDTOSelected==null)
			dataSourceDTOSelected=new DataSourceDTO();
		return dataSourceDTOSelected;
	}
	public void addDataSourceDTOsListener(DataSourceDTOsListener dataSourceDTOsListener)	{
		dataSourceDTOsListeners.add(dataSourceDTOsListener);
	}
	public void setElementListItem(String elementListItem)	{
		this.elementListItem = elementListItem;
	}
	public String getElementListItem()	{
		return elementListItem;
	}
	public void setElementName(String elementName)	{
		this.elementName = elementName;
	}
	public String getElementName()	{
		return elementName;
	}
	public void setElementListItems(ArrayList<String> elementListItems)	{
		this.elementListItems = elementListItems;
	}
	public ArrayList<String> getElementListItems()	{
		return this.elementListItems;
	}
	public void setDataSourcePresenter(DataSourcePresenter dataSourcePresenter)	{
		this.dataSourcePresenter = dataSourcePresenter;
	}
	public void executeScreenScraping(int tabNumber, final ButtonsPanel buttons)		{
		this.buttons = buttons;
		DataSourceLevelDTO dataSourceLevelDTO = dataSourceDTOSelected.getDataSourceLevelDTO(tabNumber);
		
		
		
		internetService.executeScreenScraping(dataSourceLevelDTO, dataSourceLevelDTO.getDataSourceExampleField(), dataSourceLevelDTO.getDataSourceConcat(), 
				new AsyncCallback<List<DataItemDTO>>() {
					public void onFailure(Throwable caught) {
						//dataSourcePresenter.setErrorText("Failure to Send Field to Server!!");
						buttons.addResultsTable(null);
					}

					public void onSuccess(List<DataItemDTO> results) {
						//dataSourcePresenter.setErrorText("Successfully Received LawFirms from Server!!");
						buttons.addResultsTable(results);
					}
				});		
	}

	
	public void updateDataSourceDTO()	{	
		if (dataSourceDTOSelected!=null)
			dataSourceService.updateDataSource(dataSourceDTOSelected, 
					new AsyncCallback<String>() {
						public void onFailure(Throwable caught) {
							//dataSourcePresenter.setErrorText("Failure to Send Field to Server!!");
							System.out.println("Not Success " + caught.toString());
						}
	
						public void onSuccess(String result) {
							String newName = dataSourceDTOSelected.getDataSourceName();
							dataSourceDTOSelected.setDataSourceNameKey(newName);
							//dataSourcePresenter.setErrorText("Successfully inserted Data Source!!");
						}
					});		
	}
	
	
	public void insertDataSourceDTO()	{	
		if (dataSourceDTOSelected!=null)
			dataSourceService.insertDataSource(dataSourceDTOSelected, 
					new AsyncCallback<String>() {
						public void onFailure(Throwable caught) {
							//dataSourcePresenter.setErrorText("Failure to Send Field to Server!!");
							System.out.println("Not Success " + caught.toString());
						}
	
						public void onSuccess(String result) {
							//dataSourcePresenter.setErrorText("Successfully inserted Data Source!!");
						}
					});		
	}
	
	public void retrieveDataSourceDTOs()	{	
		dataSourceService.retrieveDataSources( 
				new AsyncCallback<List<DataSourceDTO>>() {
					public void onFailure(Throwable caught) {
						//dataSourcePresenter.setErrorText("Failure to Send Field to Server!!");
					}

					public void onSuccess(List<DataSourceDTO> dataSourceDTOs) {
						setDataSourceDTOs(dataSourceDTOs);
						//dataSourcePresenter.setErrorText("Successfully retrieved Data Sources!!");
					}
				});		
		
	}
	
	public void deleteDataSourceLevelDTO(int tabNumber)	{
		dataSourceDTOSelected.removeDataSourceLevelDTO(tabNumber);
	}
	
	public void deleteDataSourceDTOs()	{	
		dataSourceService.deleteDataSource(dataSourceDTOSelected,  
				new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						//dataSourcePresenter.setErrorText("Failure to Send Field to Server!!");
					}

					public void onSuccess(String result) {
						
						setDataSourceDTOs(null);
						//dataSourcePresenter.setErrorText("Successfully retrieved Data Sources!!");
					}
				});		
		
	}
	
	
	public void beginFullDataCollection()	{	/*
		dataSourceService.startFullDataCollection( 
				new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						System.out.println("beginFullDataCollection - FAILED TRANSMISSION!!");
					}

					public void onSuccess(String status) {
						System.out.println("DataSourceModel... received status successfully - beginFullDataCollection");
					}
				});		*/
		
	}
	
	
	public void setDataSourceDTOs(List<DataSourceDTO> dataSourceDTOs)	{
		this.dataSourceDTOs = dataSourceDTOs;
		Iterator<DataSourceDTOsListener> iterator = dataSourceDTOsListeners.iterator();
		while (iterator.hasNext())	{
			DataSourceDTOsListener dataSourceDTOsListener = iterator.next();
			dataSourceDTOsListener.setDataSourceDTOs(dataSourceDTOs);
		}
	}
	@Override
	public void setDataSourceDTOSelected(DataSourceDTO dataSourceDTO, int dataSourceSelectedIndex) {
		this.dataSourceDTOSelected = dataSourceDTO;
		this.dataSourceSelectedIndex = dataSourceSelectedIndex;
	}
	
	public void setDataSourceDTOSelected(DataSourceDTO dataSourceDTO) {
		this.dataSourceDTOSelected = dataSourceDTO;
	}
	
	public DataItemLevelDTO getDataItems()	{
		return dataItemsDTO;
	}
	public void addDataSourceLevelDTO(DataSourceLevelDTO dataSourceLevelDTO)	{
		dataSourceDTOSelected.addDataSourceLevelDTO(dataSourceLevelDTO);
	}
	public void setDataSourceExecute(Boolean dataSourceExecute)	{
		if (dataSourceDTOSelected!=null)
			dataSourceDTOSelected.setDataSourceExecute(dataSourceExecute);
	}
}
